package com.lagou.client;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 编程实现客户端的初始化和关闭操作
 */

public class ClientInitClose {
    /**
     * 自定义成员变量记录Socket和流对象
     */
    private Socket s;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private BufferedWriter bw;
    private BufferedReader br;
    private PrintStream ps;

    public ObjectOutputStream getOos() {
        return oos;
    }

    public ObjectInputStream getOis() {
        return ois;
    }

    public BufferedWriter getBw() {
        return bw;
    }

    public BufferedReader getBr() {
        return br;
    }

    public PrintStream getPs() {
        return ps;
    }

    /**
     * 自定义成员方法实现客户端的初始化操作
     */
    public void ClientInit() throws IOException {

        s = new Socket(InetAddress.getLocalHost(), 8888);

        oos = new ObjectOutputStream(s.getOutputStream());
        ois = new ObjectInputStream(s.getInputStream());
        ps = new PrintStream(s.getOutputStream());
        bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        br = new BufferedReader(new InputStreamReader(s.getInputStream()));

        System.out.println("连接服务器成功");
    }

    /**
     * 自定义成员方法实现客户端的关闭操作
     */
    public void clientClose() throws IOException {
        br.close();
        bw.close();
        ps.close();
        ois.close();
        oos.close();
        s.close();
        System.out.println("客户端成功关闭");
    }
}
